using System;
using System.Collections.Generic;
using System.Windows.Forms;

using GeoFramework.IO;
using GeoFramework.Gps.Nmea;

namespace GeoFramework.Examples
{
    static class Program
    {
        public static MainForm MainExampleForm;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            MainExampleForm = new MainForm();


            /* NOTE: Trial license keys are required to make this sample work on your
             * computer.  License keys can be activated instantly by going to this
             * address: http://www.geoframeworks.com/FreeTrial.aspx.  License keys can
             * then be added in the "AssemblyInfo" file in this project.
             */

            // Start searching for GPS devices in the background
            Devices.Detect();

            // Hook into device detection events so you can see what's going on
            Devices.DeviceDetectionAttempted += new DeviceEventHandler(Devices_DeviceDetectionAttempted);
            Devices.DeviceDetectionCompleted += new EventHandler(Devices_DeviceDetectionCompleted);
            Devices.DeviceDetected += new DeviceEventHandler(Devices_DeviceDetected);

            // Start the main form as usual
            Application.Run(MainExampleForm);
        }

        private static void Devices_DeviceDetectionCompleted(object sender, EventArgs e)
        {
            // If no GPS device has been detected, offer to use an emulator
            if (!Devices.IsDeviceDetected)
            {
                switch (MessageBox.Show("No GPS devices were found to be connected to the computer.  Do you want to continue the example using a virtual GPS device?", "Use Virtual GPS Device", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    case DialogResult.Yes:
                        // Create and register a virtual GPS device
                        Devices.RegisterGpsDevice(
                            new GeoFramework.Gps.Emulators.VirtualDevice(
                            new GeoFramework.Gps.Emulators.DeLormeEarthmateUsbEmulator()));
                        break;
                }
            }
        }

        private static void Devices_DeviceDetectionAttempted(object sender, DeviceEventArgs e)
        {
            // Update the status bar
            MainExampleForm.toolStripStatusLabel1.Text = "Checking for GPS device on " + e.Device.ToString();
        }

        private static void Devices_DeviceDetected(object sender, DeviceEventArgs e)
        {
            // Update the status bar
            MainExampleForm.toolStripStatusLabel1.Text = "A GPS device was found! " + e.Device.ToString();
        }
    }
}